package com.js.rtu.fragment.second;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.js.rtu.R;
import com.js.rtu.base.BaseFragment;
import com.js.rtu.config.CmdConfig;
import com.js.rtu.util.BluetoothUtils;
import com.js.rtu.util.CmdUtils;
import com.js.rtu.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 28设置短信报警号码
 * Data： 报警手机总数+手机号码 1+…+手机号码 N
 * 报警手机总数 N： 1 字节， 十六进制数， 最大为 4 个。
 * 手机号码字段： 6 个字节， 8421 型 BCD 码，高字节在前。第一个字节最高位补 0。
 * 例如：手机号码为 13812345678 数据格式为 01 38 12 34 56 78
 * 注：报警手机总数为 0 时，表示不启用短信报警，即 Data 数据仅需要填入“0”
 * （1 byte）。
 * Created by zwz on 2020/6/22.
 */
public class Cmd_28Fragment extends BaseFragment {


    @BindView(R.id.edt_input1)
    EditText edtInput1;
    @BindView(R.id.edt_input2)
    EditText edtInput2;
    @BindView(R.id.edt_input3)
    EditText edtInput3;
    @BindView(R.id.edt_input4)
    EditText edtInput4;


    public static Cmd_28Fragment newInstance() {
        Cmd_28Fragment fragment = new Cmd_28Fragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_tab_second_cmd_28;
    }

    @Override
    protected void initView() {

    }


    private void set() {
        String phone1 = edtInput1.getText().toString();
        String phone2 = edtInput2.getText().toString();
        String phone3 = edtInput3.getText().toString();
        String phone4 = edtInput4.getText().toString();
        List<String> phoneNumber = new ArrayList<>();
        int num = 0;
        if (phone1.length() == 11) {
            //每个号码长度11位，数据格式为12，缺位补0
            phone1 = String.format(Locale.CHINA, "%012d", Long.parseLong(phone1));
            num++;
            phoneNumber.add(phone1);
        }
        if (phone2.length() == 11) {
            phone2 = String.format(Locale.CHINA, "%012d", Long.parseLong(phone2));
            num++;
            phoneNumber.add(phone2);
        }
        if (phone3.length() == 11) {
            phone3 = String.format(Locale.CHINA, "%012d", Long.parseLong(phone3));
            num++;
            phoneNumber.add(phone3);
        }
        if (phone4.length() == 11) {
            phone4 = String.format(Locale.CHINA, "%012d", Long.parseLong(phone4));
            num++;
            phoneNumber.add(phone4);
        }
        //设置的手机号码数量
        String phoneNum = String.format(Locale.CHINA, "%02d", num);
        //0个时
        if (num == 0) {
            String msg = "00";
            sendData(msg);
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(phoneNum).append(" ");
            for (String s : phoneNumber) {
                Log.e("xxxxxxx", s);
                stringBuilder.append(s.substring(0, 2)).append(" ")
                        .append(s.substring(2, 4)).append(" ")
                        .append(s.substring(4, 6)).append(" ")
                        .append(s.substring(6, 8)).append(" ")
                        .append(s.substring(8, 10)).append(" ")
                        .append(s.substring(10, 12)).append(" ");

            }


            sendData(stringBuilder.substring(0, stringBuilder.length() - 1));
        }

    }

    private void sendData(String data) {
        String cmd = CmdUtils.getCmd(CmdConfig.CMD_28, data);
        BluetoothUtils.send(cmd, new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                dismissLoadingDialog();
                String[] data = msg.split(" ");
                if (data.length < 18) {
                    ToastUtil.info(msg);
                    return;
                }
                if (data[12].equals(CmdConfig.CMD_28))
                    if (data[13].equals(CmdConfig.SUCCECC)) {
                        ToastUtil.success("设置成功");
                    } else {
                        ToastUtil.error("设置失败");
                    }

            }

            @Override
            public void onFail(String err) {
                toastShow(err);
                dismissLoadingDialog();
            }
        });
    }


    @OnClick({R.id.btn_set, R.id.tv_clean})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_set:
                set();
                break;
            case R.id.tv_clean:
                edtInput1.setText("");
                edtInput2.setText("");
                edtInput3.setText("");
                edtInput4.setText("");
                break;
        }
    }
}
